﻿Imports EVOLTA.Promocion.Bl.Bc
Imports EVOLTA.Promocion.Bl.Be

Partial Class mantenedores_motDevInmueble
    Inherits System.Web.UI.Page

    Private MotDevInmuebles As List(Of EmotDevInmueble) = Nothing
    Dim expression As String = Nothing
    Dim direction As SortDirection = SortDirection.Ascending

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.CacheControl = "no-cache"
        Response.AddHeader("Pragma", "no-cache")
        Response.Expires = -1
        '***********************************************************'
        btnAgregar.Attributes.Add("onclick", "MuestraPanel(); return false;")
        If Not IsPostBack Then
            cargarMotDevInmueblees()
            cargarEstado()
        End If
        Dim scriptText As String = ""
        scriptText &= "function mensaje(){"
        scriptText &= "    alert('se graba correctamente.');"
        scriptText &= "}"
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "mensaje()", scriptText, True)
    End Sub
    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        gvwLista.DataSource = Buscar(txtBuscar.Value.ToUpper.Trim)
        gvwLista.DataBind()
    End Sub
    Private Function Buscar(ByVal filtro As String) As IEnumerable(Of EmotDevInmueble)
        Dim LMotDevInmueble As System.Collections.Generic.IEnumerable(Of EmotDevInmueble) = Nothing
        MotDevInmuebles = Session("lista")
        If Not MotDevInmuebles Is Nothing Then
            If filtro.Length > 0 Then
                Dim x As Int32 = Asc(filtro.Substring(0, 1).Trim)
                If x >= 48 AndAlso x <= 57 Then
                    filtro = CType(filtro, Int32)
                    LMotDevInmueble = From v In MotDevInmuebles _
                                Where v.idMotDevInmueble = filtro _
                                Select v
                    Return LMotDevInmueble
                Else
                    LMotDevInmueble = From v In MotDevInmuebles _
                                Where v.descMotDevInmueble Like "*" + filtro + "*" _
                                Select v
                End If
            Else
                LMotDevInmueble = From v In MotDevInmuebles _
                            Where v.descMotDevInmueble Like "*" + filtro + "*" _
                            Select v
            End If
        End If
        Return LMotDevInmueble
    End Function
    Protected Sub lnkBtn_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) _
    Handles lnkBtnCodigo.Command, lnkBtnNombre.Command, lnkBtnEstado.Command
        Dim MotDevInmueble As IEnumerable(Of EmotDevInmueble) = Buscar(txtBuscar.Value.ToUpper.Trim)

        If Not MotDevInmueble Is Nothing Then
            Select Case e.CommandName
                Case "idMotDevInmueble"
                    Dim List = From d In MotDevInmueble _
                               Order By d.idMotDevInmueble _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "descMotDevInmueble" : expression = e.CommandArgument
                    Dim List = From d In MotDevInmueble _
                               Order By d.descMotDevInmueble _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "desEstado" : expression = e.CommandArgument
                    Dim List = From d In MotDevInmueble _
                               Order By d.estado _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
            End Select
        End If
    End Sub
    Private Sub cargarMotDevInmueblees()
        MotDevInmuebles = RmotDevInmueble.SeleccionarTodo(Me.Master.descEmpresa, False)
        Session("lista") = MotDevInmuebles
        If Not MotDevInmuebles Is Nothing Then
            '    countList = pacienteDireccion.Count
            gvwLista.DataSource = MotDevInmuebles
            gvwLista.DataBind()
            '    'gvwLista.Visible = countList > 0
        End If
    End Sub
    Private Sub cargarEstado()
        Dim Eestado As New Ecommons
        selEstado.DataSource = Eestado.estado
        selEstado.DataTextField = "descripcion"
        selEstado.DataValueField = "codigo"
        selEstado.DataBind()
    End Sub
    Protected Sub gvwLista_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwLista.PageIndexChanging

    End Sub
    Protected Sub gvwLista_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvwLista.RowCommand
        Select Case e.CommandName
            Case "Edit"
                'Dim id As String = e.CommandArgument
            Case "Delete"
                Dim MotDevInmueble As New EmotDevInmueble
                MotDevInmueble.idMotDevInmueble = e.CommandArgument
                MotDevInmueble.idUsuario = Me.Master.idUsuario
                RmotDevInmueble.Eliminar(Me.Master.descEmpresa, MotDevInmueble)
                cargarMotDevInmueblees()
        End Select
    End Sub
    Protected Sub gvwLista_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvwLista.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gvwLista, "Select$" + e.Row.RowIndex.ToString)
            e.Row.Style.Add("cursor", "hand")
        End If
    End Sub
    Protected Sub gvwLista_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwLista.RowDeleting

    End Sub
    Protected Sub gvwLista_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvwLista.RowEditing
        Dim id As Int32 = CType(CType(gvwLista.Rows(e.NewEditIndex).FindControl("lnkCodigo"), LinkButton).Text, Int32)
        MotDevInmuebles = Session("lista")
        Dim LMotDevInmueble = From v In MotDevInmuebles _
                            Where v.idMotDevInmueble.ToString = id _
                            Select v
        lblTitulo.Text = "Motivo Devolucion Inmueble [Modificar]"
        lblTitulo.ToolTip = "Modificar"
        txtId.Text = LMotDevInmueble.ElementAt(0).idMotDevInmueble
        txtDescMotDevInmueble.Value = LMotDevInmueble.ElementAt(0).descMotDevInmueble
        selEstado.SelectedIndex = LMotDevInmueble.ElementAt(0).estado
        mpDetalle.Show()
    End Sub
    Protected Sub btnGuardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        Dim MotDevInmueble As New EmotDevInmueble
        If txtDescMotDevInmueble.Value.Trim.Length > 0 Then
            Dim jsMensaje As String = Nothing
            If lblTitulo.ToolTip = "Registrar" Then
                MotDevInmueble.descMotDevInmueble = txtDescMotDevInmueble.Value.Trim.ToUpper
                MotDevInmueble.idUsuario = Me.Master.idUsuario
                Dim resultado As Boolean = _
                        RmotDevInmueble.Añadir(Me.Master.descEmpresa, MotDevInmueble)
                If resultado Then
                    jsMensaje = "<script>javascript:alert(' Se registro correctamente.');</script>)"
                    Controls.Add(New LiteralControl(jsMensaje))
                    cargarMotDevInmueblees()
                Else
                    jsMensaje = "<script>javascript:alert(' No se pudo registrar.');</script>)"
                    Controls.Add(New LiteralControl(jsMensaje))
                End If
            ElseIf lblTitulo.ToolTip = "Modificar" Then
                MotDevInmueble.idMotDevInmueble = CType(txtId.Text, Int32)
                MotDevInmueble.descMotDevInmueble = txtDescMotDevInmueble.Value.Trim.ToUpper
                MotDevInmueble.estado = Convert.ToString(selEstado.SelectedIndex)
                MotDevInmueble.idUsuario = Me.Master.idUsuario
                RmotDevInmueble.Editar(Me.Master.descEmpresa, MotDevInmueble)
                jsMensaje = "<script>javascript:alert(' Se modifico correctamente.');</script>)"
                Controls.Add(New LiteralControl(jsMensaje))
                cargarMotDevInmueblees()
            End If
            lblTitulo.ToolTip = "Registrar"
        End If
    End Sub
    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        lblTitulo.ToolTip = "Registrar"
        mpDetalle.CancelControlID = "btnCancelar"
    End Sub
    Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
        MotDevInmuebles = Session("lista")
        If Not MotDevInmuebles Is Nothing Then
            Dim fileName As String = "Consulta{0}.xls"
            fileName = String.Format(fileName, DateTime.Now.ToShortDateString())
            Dim Response As HttpResponse = HttpContext.Current.Response
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = "UTF-8"
            Response.ContentEncoding = System.Text.Encoding.Default
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)

            Response.Write("Id")
            Response.Write(vbTab)
            Response.Write("Descripción")
            Response.Write(vbTab)
            Response.Write("Estado")
            Response.Write(vbTab)
            Response.Write(vbCrLf)

            Dim value As String
            For Each MotDevInmueble In MotDevInmuebles
                value = MotDevInmueble.idMotDevInmueble
                Response.Write(value)
                Response.Write(vbTab)

                value = MotDevInmueble.descMotDevInmueble
                Response.Write(value)
                Response.Write(vbTab)

                value = MotDevInmueble.desEstado
                Response.Write(value)
                Response.Write(vbCrLf)
            Next
            Response.End()
        Else
            Dim jsMensaje As String = Nothing
            jsMensaje = "<script>javascript:alert(' La consulta no pudo ser procesada.  Por favor inténtelo de nuevo.');</script>)"
            Controls.Add(New LiteralControl(jsMensaje))
        End If
    End Sub

End Class
